package models

import "github.com/beego/beego/v2/client/orm"

//===================================================调拨主表=====================================================
type Allocation struct {
	Id             int64
	Number         string
	CreateDepId    int64
	FromDepId      int64
	FromDepName    string
	ToDepId        int64
	ToDepName      string
	ProductNumber  int64
	OperatorId     int64
	OperatorName   string
	Auditing       int //状态1待处理，2待审核，3退回，4确认调拨，5等待接收，6接收完成
	CreatedAt      int64
	CreatedBy      string
	UpdatedAt      int64
	UpdatedBy      string
	Remarks        string
	AllocationData []*AllocationData `orm:"-" json:"-"`
}

//调拨主表查询项
type AllocationQueryParam struct {
	BaseQueryParam
	Filter   string
	FromDep  string
	ToDep    string
	Deps     []interface{}
	Auditing []interface{}
}

func (a *Allocation) TableName() string {
	return AllocationTableName()
}

func AllocationTableName() string {
	return TableName("erp_allocation")
}

//查询条件
func AllocationQuerySeter(params *AllocationQueryParam) orm.QuerySeter {

	query := orm.NewOrm().QueryTable(AllocationTableName())

	cond := orm.NewCondition()

	//cond = cond.And("department_id", CurLoginUserInfo.DepartmentId)
	if len(params.FromDep) > 0 {
		cond = cond.And("from_dep_id", params.FromDep)
	}
	if len(params.ToDep) > 0 {
		cond = cond.And("to_dep_id", params.ToDep)
	}
	if len(params.Filter) > 0 { //搜索直接查规格名
		cond = cond.And("number__eq", params.Filter)
	}
	if len(params.Auditing) > 0 { //审核状态
		cond = cond.And("Auditing__in", params.Auditing)
	}
	if len(params.Deps) > 0 {
		cond = cond.And("CreateDepId__in", params.Deps)
	}
	query = query.SetCond(cond)

	return query
}

func AllocationPageList(params *AllocationQueryParam) ([]*Allocation, int64) {

	ls := make([]*Allocation, 0)

	query := AllocationQuerySeter(params)

	total, _ := query.Count()

	query.OrderBy("-Id").Limit(params.Limit, params.Offset).All(&ls)

	return ls, total
}

//仅查询
func queryAllocationTimeSoltSeter(starttime, endtime int64) orm.QuerySeter {
	query := orm.NewOrm().QueryTable(AllocationTableName())

	cond := orm.NewCondition()
	cond = cond.AndCond(cond.And("created_at__gte", starttime).And("created_at__lte", endtime))
	query = query.SetCond(cond)

	return query
}

//按时间段获取调拨
func QueryAllocationTimeSoltSeter(starttime, endtime int64) []*Allocation {

	data := make([]*Allocation, 0)

	queryStocksTimeSoltSeter(starttime, endtime).OrderBy("-id").All(&data)

	return data
}

//按时间段获取调拨count
func QueryAllocationTimeSoltSeterCount(starttime, endtime int64) int64 {

	count, _ := queryAllocationTimeSoltSeter(starttime, endtime).Count()

	return count
}

//获取调拨单
func GetAllocationById(id int64) *Allocation {
	allocation := new(Allocation)
	allocation.Id = id
	orm.NewOrm().Read(allocation)
	return allocation
}

//===================================================明细表==================================================
type AllocationData struct {
	Sid           int64           `orm:"pk"`
	Allocation    *Allocation     `orm:"rel(fk);null;on_delete(set_null)"`
	Inventory     *ErpInventory   `orm:"rel(fk);null;on_delete(set_null)"`
	Products      *ErpProducts    `orm:"rel(fk);null;on_delete(set_null)"`
	Snd           *ErpProductsSnd `orm:"rel(fk);null;on_delete(set_null)"`
	MiddleId      int64
	AllocationNum int64
}

//盘点主表查询项
type AllocationDataQueryParam struct {
	BaseQueryParam
}

func (a *AllocationData) TableName() string {
	return AllocationDataTableName()
}

func AllocationDataTableName() string {
	return TableName("erp_allocation_data")
}

//获取盘点明细
func AllocationDataList(allocation_id int64) []*AllocationData {

	ls := make([]*AllocationData, 0)

	query := orm.NewOrm().QueryTable(AllocationDataTableName()).RelatedSel("Products", "Inventory", "Inventory__Acceptance", "Snd").Filter("allocation_id", allocation_id)

	query.OrderBy("-Sid").Limit(-1).All(&ls)

	return ls
}

//查询
func AllocationDataQuerySeter(params *StocksDataQueryParam) orm.QuerySeter {
	query := orm.NewOrm().QueryTable(AllocationDataTableName())
	return query
}

//===================================================中间表==================================================
type AllocationMiddle struct {
	Id          int64        `orm:"pk"`
	Allocation  *Allocation  `orm:"rel(fk);null;on_delete(set_null)"`
	Products    *ErpProducts `orm:"rel(fk);null;on_delete(set_null)"`
	PurchaseNum int64
}

//盘点主表查询项
type AllocationMiddleQueryParam struct {
	BaseQueryParam
}

func (a *AllocationMiddle) TableName() string {
	return AllocationMiddleTableName()
}

func AllocationMiddleTableName() string {
	return TableName("erp_allocation_middle")
}

func AllocationMiddleList(allocation_id int64) []*AllocationMiddle {
	ls := make([]*AllocationMiddle, 0)
	query := orm.NewOrm().QueryTable(AllocationMiddleTableName()).RelatedSel("Products").Filter("Allocation__Id", allocation_id)
	query.OrderBy("-Id").All(&ls)
	return ls
}
